package com.xasz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xasz.entity.*;

import com.xasz.mapper.*;
import com.xasz.service.ISumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xasz.service.ex.CreatException;
import com.xasz.service.ex.DeleteException;
import com.xasz.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cai
 * @since 2022-08-01
 */
@Service
public class SumServiceImpl extends ServiceImpl<SumMapper, Sum> implements ISumService {

    @Autowired
    CandidateMapper candidateMapper;
    @Autowired
    ResultMapper resultMapper;
    @Autowired
    SumMapper sumMapper;
    @Autowired
    JuryMapper juryMapper;
    @Autowired
    ActivityMapper activityMapper;

    @Override
    public void creatSum(Integer activityId,Integer Uid) {
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>().eq("id",activityId)).getOwner()
                .equals(Uid)){
            throw new DeleteException("你没有权限创建该活动记录表");
        }
        //查询候选人列表
        QueryWrapper<Candidate> wrapper=new QueryWrapper<Candidate>()
                .eq("activity_id",activityId);
        List<Candidate> candidateList =candidateMapper.selectList(wrapper);
        for (Candidate candidate:candidateList){
            int candidateId=candidate.getId();
            //查询是否重复
            QueryWrapper<Sum> wrapperR=new QueryWrapper<Sum>()
                    .eq("activity_id",activityId)
                    .eq("candidate_id",candidateId);
            Sum results= sumMapper.selectOne(wrapperR);
            if (results!=null){
                continue;
            }
            //不重复则增加总分记录
            Sum sum=new Sum();
            sum.setActivityId(activityId);
            sum.setCandidateId(candidateId);
            int rows= sumMapper.insert(sum);
            if (rows!=1){
                throw new CreatException("总分记录生成时出现错误");
            }
        }
    }

    @Override
    public void detectSum(Integer activityId, Integer candidateId,Integer Uid) {
        //拥有者权限判断
        if (activityMapper.selectOne(new QueryWrapper<Activity>().eq("id",activityId).eq("owner",Uid))==null){
            throw new DeleteException("你没有权限删除该活动求和表");
        }
        //删除
        QueryWrapper<Sum> wrapper=new QueryWrapper<Sum>()
                .eq("activity_id",activityId)
                .eq("candidate_id",candidateId);
        if (sumMapper.delete(wrapper)!=1){
            throw new DeleteException("总分记录删除时出现错误");
        }
    }

    @Override
    public void upDataSum(Integer activityId,Integer Uid,String username) {
        //参与者权限判断
        if (juryMapper.selectOne(new QueryWrapper<Jury>().eq("activity_id",activityId).eq("name",username))==null
                && !activityMapper.selectOne(new QueryWrapper<Activity>().eq("id",activityId)).getOwner().equals(Uid)){
            throw new DeleteException("你没有权限更新该活动求和信息");
        }
        //查询候选人列表
        QueryWrapper<Candidate> wrapper=new QueryWrapper<Candidate>()
                .eq("activity_id",activityId);
        List<Candidate> candidateList =candidateMapper.selectList(wrapper);
        //每个候选人求和
        for (Candidate candidate:candidateList){
            //查询候选人的分数列表
            QueryWrapper<Result> wrapperR=new QueryWrapper<Result>()
                    .eq("activity_id",activityId)
                    .eq("candidate_id",candidate.getId());
            List<Result> results= resultMapper.selectList(wrapperR);
            //候选人的分数列表求和
            Double s = 0.0;
            for (Result result:results){
                if(result.getScore()==null){
                    continue;
                }
                s+=result.getScore();
            }
            //修改分数
            UpdateWrapper<Sum> updateWrapper=new UpdateWrapper<Sum>()
                    .eq("activity_id",activityId)
                    .eq("candidate_id",candidate.getId());
            Sum sum=new Sum();
            sum.setSum(s);
            int rows= sumMapper.update(sum,updateWrapper);
            if (rows!=1){
                throw new UpdateException("求和时出现错误");
            }
        }
    }
}
